package com.paomi.client.base;

import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.refresh.AdapterWrapper;
import com.paomi.client.refresh.SwipeToLoadHelper;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.widget.header.HeaderGetView;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by LS on 2017/5/24 0024.
 * 列表{@link RecyclerView}基类，封装了LCE loading-content-empty 切换逻辑，以及下拉刷新和加载更多
 *
 * 泛型：
 *        M 表示数据model类型
 *        P 表示presenter
 */

public abstract class BaseLCEActivity<M, P extends BaseLoadMorePresenter<BaseMvpLCEView>> extends BaseMvpActivity<BaseMvpLCEView, P> implements BaseMvpLCEView<M>,SwipeToLoadHelper.LoadMoreListener  {

    private boolean mRefreshEnable = true;

    public BaseGuideAdapter<M, BaseViewHolder> getAdapter() {
        return adapter;
    }

    public AdapterWrapper getmAdapterWrapper() {
        return mAdapterWrapper;
    }
    public SwipeToLoadHelper getmLoadMoreHelper() {
        return mLoadMoreHelper;
    }

    private AdapterWrapper mAdapterWrapper;
    private SwipeToLoadHelper mLoadMoreHelper;

    private BaseGuideAdapter<M, BaseViewHolder> adapter;
    private PtrClassicFrameLayout ptrFrameLayout;

    /**
     * 如果有下拉刷新，需要调用{@link #setPtrFrameLayout(PtrClassicFrameLayout)}
     */
    @NonNull
    protected abstract RecyclerView createRecycler();

    @CallSuper
    @Override
    protected void initEvent() {
        adapter = new BaseGuideAdapter<M, BaseViewHolder>(getItemLayout()) {
            @Override
            public void initEmpty(TextView empty) {
                BaseLCEActivity.this.initEmpty(empty);
            }

            @Override
            protected void convert(BaseViewHolder helper, M item) {
                BaseLCEActivity.this.convert(helper, item);
            }
        };
        RecyclerView mRecycler = createRecycler();
        if(mRecycler!=null){
            mRecycler.setLayoutManager(createLayoutManager());

            adapter.bindToRecyclerView(mRecycler);
            mAdapterWrapper = new AdapterWrapper(adapter);
            mLoadMoreHelper = new SwipeToLoadHelper(mRecycler, mAdapterWrapper);
            mLoadMoreHelper.setLoadMoreListener(this);

            mRecycler.setAdapter(mAdapterWrapper);
            //以前的上拉加载不可用
//            mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
////                @Override
////                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
////                    super.onScrollStateChanged(recyclerView, newState);
////                    adapter.setLoadMoreListener(new BaseGuideAdapter.OnLoadMoreListener() {
////                        @Override
////                        public void onLoadMoreRequested() {
////                            loadData(false);
////                        }
////                    }, mRecycler);
////                }
////
////                @Override
////                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
////                    super.onScrolled(recyclerView, dx, dy);
////                }
////            });
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter a, View view, int position) {
                    BaseLCEActivity.this.onItemClick(view,adapter.getItem(position));
                }
            });
        }

        if (ptrFrameLayout != null) {
            HeaderGetView header=new HeaderGetView(this);
            ptrFrameLayout.setHeaderView(header);
            ptrFrameLayout.addPtrUIHandler(header);
            ptrFrameLayout.setLastUpdateTimeRelateObject(this);
            ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {
                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    loadData(true);
                }

                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    return mRefreshEnable && super.checkCanDoRefresh(frame, content, header);   // 是否需要支持下拉刷新
                }
            });
            ptrFrameLayout.autoRefresh();
        } else {
            loadData(true);
        }
    }

    @NonNull
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new LinearLayoutManager(this);
    }

    /**
     * 列表条目布局
     */
    protected abstract int getItemLayout();
    /**
     * 列表无数据的布局
     */
    protected abstract void initEmpty(TextView empty);
//    empty.setText(R.string.empty_record);
//    empty.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.empty_packet,0,0);
    /**
     * 列表布局填充
     */
    protected abstract void convert(BaseViewHolder helper, M item);
    /**
     * 列表条目点击
     */
    protected abstract void onItemClick(View view, M item);

    @Override
    public void loadData(boolean pullToRefresh) {
        getPresenter().loadData(pullToRefresh);
    }

    @Override
    public void setData(List<M> data, boolean pullToRefresh) {
        LoadingDialog.get().hideLoading();
        if(pullToRefresh && ptrFrameLayout != null) ptrFrameLayout.refreshComplete();
        adapter.setDatas(data);
    }

    protected void setPtrFrameLayout(PtrClassicFrameLayout ptrFrameLayout) {
        this.ptrFrameLayout = ptrFrameLayout;
    }

    public void setPullToRefreshEnable(boolean refreshEnable) {
        this.mRefreshEnable = refreshEnable;
    }

    @Override
    public void onLoad() {
    }
}
